from django.test import TestCase
from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser

from app.models import Category, Product, ProductSpec


class ProductTest(TestCase):
    def setUp(self):
        category = Category(name=u'蔬菜')
        category.save()
        self.category_pk = category.pk

        product = Product(name=u'土豆',
                          description=u'可以吃的')
        product.save()
        product.category.add(category)
        product.save()
        ProductSpec(product=product,
                    spec_name=u'小土豆',
                    price=100,
                    unit=u'斤').save()
        ProductSpec(product=product,
                    spec_name=u'大土豆',
                    price=150,
                    unit=u'斤').save()

    def test_product_list(self):
        response = self.client.get(f'/api/category/{self.category_pk}/product.json')
        self.assertEqual(response.status_code, 200)
        data = JSONParser().parse(BytesIO(response.content))
        self.assertTrue(data)

    def test_product_detail(self):
        pass
